﻿@inject IWebHostEnvironment even
@inject NavigationManager nav
@inject ToastService ts

<AvatarUpload TValue="string" OnChange="@ChangFiles" IsSingle="true"></AvatarUpload>

@code {
    [Parameter]
    public Image? Image { get; set; }

    [Parameter]
    public EventCallback<Image?> ImageChanged { get; set; }

    private async Task ChangFiles(UploadFile file)
    {
        if(file.Size > 512 * 1024)
        {
            await ts.Error("警告!", "图片尺寸不能大于512kb", true);
            return;
        }

        if(Image is not null && file is not null)
        {
            if(file.File is not null)
            {
                Image.FileName = $"{Image.Id}{Path.GetExtension(file.File.Name)}";
                string path = Path.Combine(even.WebRootPath, "images", Image.FileName);
                await file.SaveToFileAsync(path,file.Size);
                Image.Url = $"{nav.BaseUri}images/{Image.FileName}";
                file.PrevUrl = Image.Url;
            }
        }
    }
}
